package com.movitech.mobile.cz.base.supplier.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.IdGenUtils;
import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.modules.config.HistoryCommentModelTypeEnum;
import com.movitech.mobile.cz.base.modules.config.HistoryCommentTypeEnum;
import com.movitech.mobile.cz.base.supplier.bizvo.OverAllConfirmHistoryRequestVO;
import com.movitech.mobile.cz.base.supplier.bizvo.PurchasePlanConfirmHistoryRequestVO;
import com.movitech.mobile.cz.base.supplier.entity.SHistoryComment;
import com.movitech.mobile.cz.base.supplier.entity.ext.ExtSHistoryComment;
import com.movitech.mobile.cz.base.supplier.entity.ext.RankHistoryReqVO;
import com.movitech.mobile.cz.base.supplier.entity.ext.SHistoryCommentFindRequest;
import com.movitech.mobile.cz.base.supplier.mapper.SHistoryCommentMapper;
import com.movitech.mobile.cz.base.supplier.mapper.ext.ExtSHistoryCommentMapper;
import com.movitech.mobile.cz.base.supplier.service.SHistoryCommentService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 审批历程（审批历史） 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@Service("sHistoryCommentService")
@Transactional
public class SHistoryCommentServiceImpl extends ServiceImpl<SHistoryCommentMapper, SHistoryComment> implements SHistoryCommentService {

    @Autowired
    private SHistoryCommentMapper sHistoryCommentMapper;

	@Autowired
	private ExtSHistoryCommentMapper esHistoryCommentMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackForClassName = { "RuntimeException", "Exception" })
    @Override
    public boolean saveSHistoryComment(SHistoryComment sHistoryComment) {
    	if(StringUtils.isEmpty(sHistoryComment.getId())){
    		sHistoryComment.setId(IdGenUtils.uuid());
    	}
		
		Date createDate = new Date();
		String userId = UserUtils.getCurrentUserId();
		sHistoryComment.setSetupTime(createDate);
		sHistoryComment.setCreateBy(userId);
		sHistoryComment.setCreateDate(createDate);
		sHistoryComment.setUpdateBy(userId);
		sHistoryComment.setUpdateDate(createDate);
		sHistoryComment.setDelFlag(CommonConstants.NO_INTEGER);
		
		int i = sHistoryCommentMapper.insert(sHistoryComment);
		
        return i > 0;
    }

    @Transactional(readOnly = false)
	@Override
	public boolean updateSHistoryCommentById(SHistoryComment sHistoryComment) {
		
		Date updateDate = new Date();
		String userId = UserUtils.getCurrentUserId();
		sHistoryComment.setSetupTime(updateDate);
		sHistoryComment.setUpdateBy(userId);
		sHistoryComment.setUpdateDate(updateDate);
		sHistoryComment.setDelFlag(CommonConstants.NO_INTEGER);
		
		int i = sHistoryCommentMapper.updateById(sHistoryComment);
		
		return i > 0;
	}


	@Override
	public  Page<ExtSHistoryComment> findConfirmHistory(Page<ExtSHistoryComment> page,PurchasePlanConfirmHistoryRequestVO vo) {
    	Map<String,Object> map = new HashMap<String, Object>();
		map.put("modelType", HistoryCommentModelTypeEnum.HistoryCommentModelType6.getCode());
		map.put("commentType", HistoryCommentTypeEnum.INTRANET.getCode());
		map.put("projectId", vo.getProjectId());
		map.put("regionalCompanyId", vo.getRegionalCompanyId());
		List<ExtSHistoryComment> data =esHistoryCommentMapper.findPsPPlanConfirmHistoryList2(page, map);
		page.setRecords(data);
		return page;
	}

	@Override
	public Page<ExtSHistoryComment> findConfirmHistory(Page<ExtSHistoryComment> page, OverAllConfirmHistoryRequestVO vo) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("modelType", HistoryCommentModelTypeEnum.HistoryCommentModelType28.getCode());
		map.put("commentType", HistoryCommentTypeEnum.INTRANET.getCode());
		map.put("year", vo.getYear());
		List<ExtSHistoryComment> data = esHistoryCommentMapper.findOverAllConfirmHistory(page,map);
		page.setRecords(data);
		return page;
	}

	@Override
	public List<SHistoryComment> findByModelTypeAndRelatedId(Integer type, String relateId, String subRelateId) {
		EntityWrapper entityWrapper = new EntityWrapper();
		SHistoryComment sHistoryComment = new SHistoryComment();
		sHistoryComment.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
		sHistoryComment.setModelType(type);
		sHistoryComment.setRelatedId(relateId);
		sHistoryComment.setSubRelatedId(subRelateId);
		entityWrapper.setEntity(sHistoryComment);
		entityWrapper.orderBy("create_date",false);
		return selectList(entityWrapper);
	}

	@Override
	public List<ExtSHistoryComment> findExtByModelTypeAndRelatedId(Integer type, String relateId, String subRelateId) {
		SHistoryCommentFindRequest sHistoryCommentFindRequest = new SHistoryCommentFindRequest();
		sHistoryCommentFindRequest.setRelatedId(relateId);
		sHistoryCommentFindRequest.setSubRelateId(subRelateId);
		sHistoryCommentFindRequest.setModelType(type);
		return esHistoryCommentMapper.getExtByEntity(sHistoryCommentFindRequest);
	}

	@Override
	public PageCode historyCommentList(RankHistoryReqVO rankHistoryReqVO) {
		PageCode pageCode = new PageCode(rankHistoryReqVO.getPageNumber(), rankHistoryReqVO.getPageSize());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rankYear", rankHistoryReqVO.getRankYear());
		map.put("modelType", HistoryCommentModelTypeEnum.HistoryCommentModelType29.getCode());
		pageCode.setCount(esHistoryCommentMapper.historyCommentCount(map));

		map.put("currentRecordNum", (pageCode.getPageNo() - 1) * pageCode.getPageSize());
		map.put("pageSize", pageCode.getPageSize());

		List<ExtSHistoryComment> ls = esHistoryCommentMapper.historyCommentList(map);
		pageCode.setList(ls);
		return pageCode;
	}

}
